<?php

class Banners extends Admin_Controller {

	function __construct()
	{
		parent::__construct();
		
		$this->aPageJs[] = array(
							'combine_js' => true,
							'base_href' => '/media/js',
							'js' => array('admin_banners'),
							'version' => '1.1'
		);	

		// build up our header_css array
		$this->aPageCss[] = array(
							'combine_css' => true,
							'base_href' => '/media/css',
							'css' => array('admin_banners'),
							'media' => 'all',
							'version' => '1.1'
		);
		
		$this->load->helper('form');
		$this->load->library('form_validation');
	}
	
	public function index()
	{
		$this->load->model('db/baa_banners', 'baa_banners');
		$this->baa_banners->banner_status = 'LI';
		$qBanners = $this->baa_banners->get();
		
		$this->aData['qBanners'] = $qBanners;
		
		$this->load->view('platform/'.$this->sPlatform.'/admin/banners/index', $this->aData);
	}
	
	public function add_banner()
	{
		$this->edit_banner(NULL);
	}
	
	public function edit_banner($iBannerId = NULL, $bSaved = false)
	{
		$this->load->model('db/baa_banners', 'baa_banners');
		
		$oBanner = $this->baa_banners->get_object();
		
		$this->load->model('db/views/media_by_folders', 'vw_media_by_folders');
		
		$oBanner->oMedia = $this->vw_media_by_folders->get_object();
		
		if($iBannerId !== NULL)
		{
			$this->baa_banners->banner_id = $iBannerId;
			
			$qBanner = $this->baa_banners->get(1);
			
			$oBanner = $qBanner->num_rows() === 1 ? $qBanner->row(0) : $oBanner;
			
			$this->vw_media_by_folders->media_id = $oBanner->image_id;
			$this->vw_media_by_folders->dimension_type = 'W';
			$qMedia = $this->vw_media_by_folders->get(1);
			$oBanner->oMedia = $qMedia->num_rows() === 1 ? $qMedia->row(0) : $this->vw_media_by_folders->get_object();
		}
		
		$this->aData['oBanner'] = $oBanner;
		
		if($bSaved)
		{
			$this->aPageJs[] = array(
								'combine_js' => true,
								'base_href' => '/media/js',
								'js' => array('admin_banner_saved'),
								'version' => '1.1'
			);
		}
		
		$this->load->view('platform/'.$this->sPlatform.'/admin/banners/edit', $this->aData);
	}
	
	public function save_banner()
	{
		$banner_id = $this->input->post('banner_id', true, 0);
		$banner_name = $this->input->post('banner_name', true, '');
		
		$banner_image = $this->input->post('banner_image', true, 0);
		
		$aErrors = array();
		
		if($banner_name == '')
		{
			$aErrors[] = 'Please complete the Banner name field.';
		}
		
		if(!$banner_image > 0)
		{
			$aErrors[] = 'Please select an Image.';
		}
		
		if(count($aErrors) > 0)
		{
			$aVars = $_POST;
			$aVars['errors'] = '<div class="ep_error">'.implode('</div><div class="ep_error">', $aErrors).'</div>';
			
			$this->load->vars($aVars);
			
			$this->edit_banner();
			
			return;
		}
		
		$this->load->model('db/baa_banners', 'baa_banners');
		
		if($banner_id > 0)
		{
			$this->baa_banners->banner_id = $banner_id;
		}
		
		$this->baa_banners->banner_name = $banner_name;
		$this->baa_banners->banner_status = 'LI';
		$this->baa_banners->image_id = $banner_image;
		
		$banner_id = $this->baa_banners->save(false);
		
		$message = 'Your Banner has been saved';
		$this->load->vars(array('message' => $message));
		
		$this->edit_banner($banner_id, true);
	}
	
}

?>